// ...........................................................................................................
// (C) Copyright  1996/2008 Fuego Inc.  All Rights Reserved
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Fuego Inc.
// The copyright notice above does not evidence any actual or intended
// publication of such source code.
//
// Last changed on 2008-01-11 16:42:36 (-0300), by: machaval. $Revision$
// ...........................................................................................................

package com.webcamtracker.video.codecs;

import com.webcamtracker.video.VideoManager;
import com.webcamtracker.video.VideoSuscriptionSupport;
import com.webcamtracker.video.VideoSuscriptor;

import javax.media.Buffer;
import javax.media.Format;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageProviderCodec
        extends AccessCodec {
    //~ Instance fields ......................................................................................

    private VideoSuscriptionSupport videoSuscriptionSupport;
    private VideoManager manager;
    private Type type = Type.WebCam;
    //~ Constructors .........................................................................................

    public ImageProviderCodec() {
        this.videoSuscriptionSupport = new VideoSuscriptionSupport();
    }

    //~ Methods ..............................................................................................

    public void addVideoSuscriptor(VideoSuscriptor videoSuscriptor) {
        videoSuscriptionSupport.addVideoSuscriptor(videoSuscriptor);
    }


    public Format[] getSupportedOutputFormats(Format in) {
        if (in == null) {
            return supportedOuts;
        } else {
            // If an input format is given, we use that input in
            // as the output since we are not modifying the bit stream
            // at all.
            Format[] outs = new Format[1];
            outs[0] = in;
            return outs;
        }
    }

    public void setManager(VideoManager manager) {
        this.manager = manager;
    }

    public String getName() {
        return "Image Provider Codec";
    }

    void accessFrame(Buffer frame) {
        BufferedImage buffImg = getImage(frame);

        if (buffImg != null) {
            videoSuscriptionSupport.deliveryFrame(buffImg);
        }
    }

    public void setType(Type type) {
        this.type = type;
    }

    private BufferedImage getImage(Buffer in) {
        if (type == Type.WebCam) {
            Image img = (new BufferToImage((VideoFormat) in.getFormat()).createImage(in));
            BufferedImage buffImg = null;

            if (img != null) {
                buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
                Graphics2D g = buffImg.createGraphics();
                g.drawImage(img, null, null);
            }

            return buffImg;
        } else {
            if (manager != null) {
                return manager.getImage();
            }
            return null;
        }
    }

    public static enum Type {
        Video, WebCam
    }
}
